package flc.ast.fragment;

import android.graphics.Color;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.activity.CategoryActivity;
import flc.ast.activity.MovieDetailActivity;
import flc.ast.activity.MovieRankActivity;
import flc.ast.activity.PlayletRankActivity;
import flc.ast.activity.SearchActivity;
import flc.ast.adapter.HotAdapter;
import flc.ast.adapter.MovieRecAdapter;
import flc.ast.databinding.FragmentHomeBinding;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;
import stark.common.bean.StkTagResBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends BaseNoModelFragment<FragmentHomeBinding> {

    private HotAdapter movieHotAdapter;
    private HotAdapter playletHotAdapter;
    private MovieRecAdapter movieRecAdapter;
    private MovieRecAdapter playletRecAdapter;
    private String movieHashId;
    private String playletHashId;

    @Override
    protected int onCreate() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.ivSearch.setOnClickListener(this);
        mDataBinding.ivCategory.setOnClickListener(this);
        mDataBinding.tvMovie.setOnClickListener(this);
        mDataBinding.tvPlaylet.setOnClickListener(this);
        mDataBinding.ivMovieHotMore.setOnClickListener(this);
        mDataBinding.ivPlayletHotMore.setOnClickListener(this);
        mDataBinding.ivTop.setOnClickListener(this);

        mDataBinding.rvMovieHotList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        movieHotAdapter = new HotAdapter();
        mDataBinding.rvMovieHotList.setAdapter(movieHotAdapter);
        movieHotAdapter.setOnItemClickListener(this);

        mDataBinding.rvPlayletHotList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        playletHotAdapter = new HotAdapter();
        mDataBinding.rvPlayletHotList.setAdapter(playletHotAdapter);
        playletHotAdapter.setOnItemClickListener(this);

        mDataBinding.rvMovieRecList.setLayoutManager(new GridLayoutManager(mContext, 3));
        movieRecAdapter = new MovieRecAdapter();
        mDataBinding.rvMovieRecList.setAdapter(movieRecAdapter);
        movieRecAdapter.setOnItemClickListener(this);

        mDataBinding.rvPlayletRecList.setLayoutManager(new GridLayoutManager(mContext, 3));
        playletRecAdapter = new MovieRecAdapter();
        mDataBinding.rvPlayletRecList.setAdapter(playletRecAdapter);
        playletRecAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivSearch://搜素
                startActivity(SearchActivity.class);
                break;
            case R.id.ivCategory://分类
                startActivity(CategoryActivity.class);
                break;
            case R.id.tvMovie://影视剧场
                clearSelection();
                mDataBinding.tvMovie.setTextColor(Color.parseColor("#FFFFFF"));
                mDataBinding.ivMovieSel.setVisibility(View.VISIBLE);
                mDataBinding.llMovie.setVisibility(View.VISIBLE);
                break;
            case R.id.tvPlaylet://短剧剧场
                clearSelection();
                mDataBinding.tvPlaylet.setTextColor(Color.parseColor("#FFFFFF"));
                mDataBinding.ivPlayletSel.setVisibility(View.VISIBLE);
                mDataBinding.llPlaylet.setVisibility(View.VISIBLE);
                break;
            case R.id.ivMovieHotMore://热播影视榜
                MovieRankActivity.url = movieHashId;
                startActivity(MovieRankActivity.class);
                break;
            case R.id.ivPlayletHotMore://热播排行榜
                PlayletRankActivity.url = playletHashId;
                startActivity(PlayletRankActivity.class);
                break;
            case R.id.ivTop://置顶
                mDataBinding.scrollView.fullScroll(View.FOCUS_UP);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        StkResBean bean = (StkResBean) adapter.getItem(position);
        MovieDetailActivity.stkResBean = bean;
        startActivity(MovieDetailActivity.class);
    }

    @Override
    protected void initData() {
        getMovieData();
        getPlayletData();
    }

    private void getMovieData() {
        StkResApi.getChildTagList(this, AppConfig.MOVIE_DATA, StkResApi.createParamMap(0, 4), false, new IReqRetCallback<List<StkTagResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkTagResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        movieHashId = result.get(0).getHashid();
                        getMovieHotData(result.get(0).getHashid());
                        getMovieRecData(result.get(1).getHashid());
                    }
                }
            }
        });
    }

    private void getMovieHotData(String url) {
        StkResApi.getTagResourceList(this, AppConfig.URL_PREFIX + url, StkResApi.createParamMap(0, 5), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        movieHotAdapter.setList(result);
                    }
                }
            }
        });
    }

    private void getMovieRecData(String url) {
        StkResApi.getTagResourceList(this, AppConfig.URL_PREFIX + url, StkResApi.createParamMap(0, 9), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        movieRecAdapter.setList(result);
                    }
                }
            }
        });
    }

    private void getPlayletData() {
        StkResApi.getChildTagList(this, AppConfig.PLAYLET_DATA, StkResApi.createParamMap(0, 4), false, new IReqRetCallback<List<StkTagResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkTagResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        playletHashId = result.get(0).getHashid();
                        getPlayletHotData(result.get(0).getHashid());
                        getPlayletRecData(result.get(1).getHashid());
                    }
                }
            }
        });
    }

    private void getPlayletHotData(String url) {
        StkResApi.getTagResourceList(this, AppConfig.URL_PREFIX + url, StkResApi.createParamMap(0, 5), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        playletHotAdapter.setList(result);
                    }
                }
            }
        });
    }

    private void getPlayletRecData(String url) {
        StkResApi.getTagResourceList(this, AppConfig.URL_PREFIX + url, StkResApi.createParamMap(0, 9), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        playletRecAdapter.setList(result);
                    }
                }
            }
        });
    }

    private void clearSelection() {
        mDataBinding.tvMovie.setTextColor(Color.parseColor("#979590"));
        mDataBinding.ivMovieSel.setVisibility(View.INVISIBLE);
        mDataBinding.llMovie.setVisibility(View.GONE);
        mDataBinding.tvPlaylet.setTextColor(Color.parseColor("#979590"));
        mDataBinding.ivPlayletSel.setVisibility(View.INVISIBLE);
        mDataBinding.llPlaylet.setVisibility(View.GONE);

    }

}